package com.igeek.d0604;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @Author: zhiyu
 * @Date:16:32
 * @DESC:
 */
public class InterfaceTest {
    public static void main(String[] args) {
        int[] arr = {2,1,43,1,34,46,23,-21};
        supplierTest(()->arr);
        String[] array = { "张三,女", "李四,女", "王二麻,男" };
        consumerTest(string -> System.out.print("姓名："+string.split(",")[0]+"。"),
                string -> System.out.println("性别："+string.split(",")[1]+"。"),array);
        String str = "张三,20";
        functionTest(string->Integer.parseInt(string), integer-> integer,str);
        String[] array1 = { "张三,男", "李四,女", "王二麻子,女" };
        System.out.println("filter(string->string.length() == 4, string->string.equals(\"女\"),array1) = " + filter(string -> string.length() == 4, string -> string.equals("女"), array1));
    }
    public static void supplierTest(Supplier<int[]> supplier){
        int[] ints = supplier.get();
        Arrays.sort(ints);
        System.out.println(ints[ints.length-1]);
    }
    public static void consumerTest(Consumer<String> name,Consumer<String> gender,String[] array){
        for (int i = 0; i < array.length; i++) {
            name.andThen(gender).accept(array[i]);
        }
    }
    public static void functionTest(Function<String,Integer> age,Function<Integer,Integer> result,String string){
        String[] split = string.split(",");
        System.out.println("result.apply(age.apply(split[1])+100) = " + result.apply(age.apply(split[1]) + 100));
    }
    public static ArrayList<String> filter(Predicate<String> name, Predicate<String> gender, String[] array){
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            String[] split = array[i].split(",");
            if (name.test(split[0]) && gender.test(split[1])){
                list.addAll(Arrays.asList(split));
            }
        }
        return list;
    }
}
